// import "@/utils/http";

const BASE_URL = 'http://192.168.1.18:30000/xinggc';

export function post(url = "", data = {}) {
	const header = {
		'content-type': "application/x-www-form-urlencoded"
	};
	return request(url, "POST", header, data);
}

export function jsonPost(url = "", data = {}) {
	return request(url, "POST", {}, data);
}

function request(url = "", method = "", header = {}, data = {}) {
	url = BASE_URL + url;

	const userinfo = uni.getStorageSync("userinfo");
	const token = userinfo?.token;
	if (token) {
		header.Authorization = token;
	}
	const companyId = userinfo?.userMap?.companyId;
	if (companyId) {
		data.companyId = companyId;
	}

	console.log("发送 url: ", url);
	console.log("发送 data: ", data);

	return new Promise((resolve, reject) => {
		uni.request({
			url,
			method,
			header,
			data,
			success: res => {
				if (res.statusCode === 200) {
					if (res.data.code === 10000) {
						resolve(res.data.data)
						console.log("请求成功 data: ", res.data.data);
					} else {
						uni.showModal({
							title: "错误提示",
							content: res.data.desc,
							showCancel: false
						})
						console.log("请求失败 err: ", err);
						reject(res.data.desc)
					}
				} else {
					let err = res.data.error + ' (' + res.data.status + ')';
					uni.showModal({
						title: "错误提示",
						content: err,
						showCancel: false
					})
					console.log("请求失败 err: ", err);
					reject(err)
				}
			},
			fail: err => {
				console.log("请求失败 err: ", err);
				reject(err)
			}
		})
	})
}